package org.NooLab.itexx.retrieval;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

import org.NooLab.utilities.logging.PrintLog;
import org.NooLab.utilities.strings.StringsUtil;

  

/**
 * 
 * this is the container for a single "item", which starts like this
 * 
 * <item name="google" target="www.google.*" mode="text">
 * ...
 * </item>
 * 
 * the file is a catalog consisting of such item descriptions
 * 
 * 
 */
 
public class PreProcRuleSet implements Serializable{
 
	private static final long serialVersionUID = -3590036477326331142L;
	// =================================
	
	// object references ..............
	

	// main variables / properties ....

	
	// constants ......................
	
	
	// volatile variables .............

	
	// helper objects .................
	PrintLog out  ;

	String name ="";
	
	ArrayList<ruleItem> excluded  = new ArrayList<ruleItem>() ;
	
	ArrayList<ruleItem> mandatory = new ArrayList<ruleItem>() ;

	ArrayList<ruleItem> avoided   = new ArrayList<ruleItem>() ;
	
	ArrayList<ruleItem> preferred = new ArrayList<ruleItem>() ;
	
	ArrayList<ruleItem> files     = new ArrayList<ruleItem>() ;
	
	Vector<ruleItem> textItems    = new Vector<ruleItem>() ;
	String text ;
	
	RuleEditMode mode = RuleEditMode.additive;
	
	StringsUtil strgutil ;
	
	
	
	public PreProcRuleSet(){
		 
		
	}

	public enum RuleEditMode { additive, replace };
	 
	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public ArrayList<ruleItem> getExcluded() {
		return excluded;
	}


	
	private void additems( ArrayList<ruleItem> ruleitems, ArrayList<String> strlist){
		String str ;
		ruleItem item;
		
		for (int i=0;i<strlist.size();i++){
			str = strlist.get(i).trim() ;
			
			if ((str!=null) && (str.length()>0)){
				item = new ruleItem();
				item.label = str;
				ruleitems.add(item) ;
			}
		}
		
	}
	
	public void setExcluded(ArrayList<String> arrayList) {
		// dependent on RuleEditMode mode !!!
		int n;
		
		additems( excluded, arrayList);
		 
		n = excluded.size();
 		
	}

	public void setAvoided(ArrayList<String> strlist) {
		 
		additems( avoided, strlist);
		 
	}
	public void setPreferred(ArrayList<String> strlist) {
		 
		additems( preferred, strlist);
		 
	}
 

	public ArrayList<ruleItem> getMandatory() {
		return mandatory;
	}


	public void setMandatory( ArrayList<String> strlist) {
		 
		int n;
		
		additems( mandatory, strlist);
		 
		n = mandatory.size();
	}


	public ArrayList<ruleItem> getFiles() {
		return files;
	}


	public void setFiles(ArrayList<String> strlist) {
		 
		additems( files, strlist);
	}


	public Vector<ruleItem> getText() {
		return textItems;
	}


	public void setText( ArrayList<String> strlist) {
		 
		int n;
		
		additems( mandatory, strlist);
		 
		n = textItems.size();
	}
	
	public void setText(String str) {
		String[] strtokens;
		List<String> tL ;
		ArrayList<String> strlist = new ArrayList<String>();
		
		str = strgutil.replaceAll(str, "  ", " ") ;
		strtokens = str.split(" ");
		
		tL = Arrays.asList(strtokens);
		strlist.addAll(tL) ;
		setText(strlist) ;
	}


	public RuleEditMode getMode() {
		return mode;
	}


	public void setMode(RuleEditMode mode) {
		this.mode = mode;
	}
	
	public void setMode( int m) {
		if (m<=1){
			mode = RuleEditMode.additive;
		}
		if (m>=2){
			mode = RuleEditMode.replace;
		}
	}

	protected void setObj( StringsUtil strutil ){
		strgutil = strutil;
	}
}

class ruleItem implements Serializable{
	 
	private static final long serialVersionUID = -4979635603146503882L;
	
	String label;
	
	double weight = 1.0;
	boolean active = true;
	
	
	public String getLabel() {
		return label;
	}
	public void setLabel(String label) {
		this.label = label;
	}
	 
	public double getWeight() {
		return weight;
	}
	public void setWeight(double weight) {
		this.weight = weight;
	}
	public boolean isActive() {
		return active;
	}
	public void setActive(boolean active) {
		this.active = active;
	}
	

}













